<?php
/**
 * rakuten 连接请求
 * User: bgl
 * Date: 2022/9/13
 * Time: 14:26
 */

namespace App\Libs\Rakuten\Common;


use App\Models\Basics\RakutenAccount;
use Exception;

class RakutenClient{

    //乐天配置（rakuten秘钥）
    const CONTENT_TYPE = "Content-Type: application/json; charset=utf-8";
    const API_URL = "https://api.rms.rakuten.co.jp"; //api地址

    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M19DUnMwUVlaS0FQWkY0NHln"; //到期时间：2022-12-08（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M194RW90M1BiVVA5Yjg1bmlW"; //到期时间：2023-03-12（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M18wWXpnaGN0czBvUkhyVHZO"; //到期时间：2023-06-13（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M19OQWVrU0ZZcG1IYnBpRmpp"; //到期时间：2023/09/07（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M19sSGVhN2NEQURoT3p5clNk"; //到期时间：2023/12/10（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M19TaklWcGFqQkNUWkc3S2Vr"; //到期时间：2024/02/27（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M18xSFgybnFraUI3dFpZa0JZ"; //到期时间：2024/05/27（已过期）
    //const AUTHORIZATION = "Authorization: ESA U1A0MDcwODNfeEUxdFU0OHZZRmFjcERQTzpTTDQwNzA4M19lREViWlJISGx0dTgzUFJT"; //到期时间：2024/06/25

    /**
     * $apiParams
     * @param $method
     * @param $apiParams
     * @param $account
     * @return RakutenResponse|bool|string
     */
    public function executePost($method, $apiParams, $account){

        $response = new RakutenResponse();

        //请求地址
        $requestUrl = self::API_URL ."/". $method;
        try {
            //发起请求
            $result = $this->curlPost($requestUrl,$apiParams,$account);
        }catch (Exception $e) {
            $response->code = $e->getCode();
            $response->errormsg = $e->getMessage();
            return $response;
        }
        return $result;
    }

    /**
     * curl请求(post)
     * @param $url
     * @param $apiParams
     * @param $account
     * @return bool|string
     */
    public function curlPost($url,$apiParams,$account){

        $authorization = "Authorization: ESA ".$account->authorization;
        $ch = curl_init($url); //开启CURL会话

        curl_setopt($ch, CURLOPT_HTTPHEADER, [self::CONTENT_TYPE, $authorization]); //设置header头信息
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_POST, true); //发起Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($apiParams)); //Post提交的数据
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //信息以文件流的形式返回
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); //设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096); //设置缓冲区大小
        
        $result = curl_exec($ch); //curl执行
        
        curl_close($ch); //curl关闭
        
        return $result;
    }

    /**
     * 获取请求
     * @param $apiUrl
     * @param $apiParams
     * @param $account
     * @return mixed
     */
    public static function getRequest($apiUrl,$apiParams,$account = null){
        if (!$account) {
            $account = RakutenAccount::getAccount(['auth_status' => 1]); //默认账号：200000
        }
        
        $obj = new RakutenClient();
        $res = $obj->executePost($apiUrl,$apiParams,$account);
        return json_decode($res);
    }

    /**
     * get
     * @param $method
     * @param $apiParams
     * @return RakutenResponse|bool|mixed|string
     */
    /*public function executeGet($method){

        $result = new RakutenResponse();
        //请求地址
        $requestUrl = self::API_URL ."/". $method;
        try {
            $response = $this->curlGet($requestUrl);
        }catch (Exception $e) {
            $result->code = $e->getCode();
            $result->errormsg = $e->getMessage();
            return $result;
        }
        return $response;
    }*/

    /**
     * curl请求(get)
     * @param $url
     * @return mixed
     */
    /*public function curlGet($url){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTPHEADER, [self::CONTENT_TYPE, self::AUTHORIZATION]); //设置header头信息
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //信息以文件流的形式返回
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); //认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); //设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }*/

}
